package com.jy.wms.controller;

import com.github.pagehelper.Page;

import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.UUIDUtils;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.ReturnType;
import com.jy.wms.common.base.ServiceException;

import com.jy.wms.common.base.SucessException;
import com.jy.wms.pojo.SysUser;
import com.jy.wms.pojo.WmsShelfRule;
import com.jy.wms.service.WmsShelfRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: WmsShelfRuleController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author lh
 * @Date Mon Sep 04 11:27:57 GMT+08:00 2017
 */

@RestController
@RequestMapping("/wmsShelfRule")
@Api(value = "/wmsShelfRule", description = "关于上架规则操作")
public class WmsShelfRuleController extends BaseController<WmsShelfRuleService, WmsShelfRule> {


    @ApiOperation(value = "查询上架规则列表", response = JSONObject.class, notes = "查询上架规则列表")
    @PostMapping(value = "/list")
    public @ResponseBody
    JSONObject getWmsShelfRule(@RequestBody WmsShelfRule entity, HttpSession session) {
        //JSONObject json = new JSONObject();
        JSONObject json = getJSONObject();
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        try {
            Page<WmsShelfRule> list = baseService.baseSelectPage(entity,ckid);
             json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
        }catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    @ApiOperation(value = "查询所有上架规则", response = JSONObject.class, notes = "查询所有上架规则")
    @GetMapping(value = "/getDictionary")
    public @ResponseBody
    JSONArray getDictionary(HttpSession session)throws Exception {
        JSONArray jsonArray = new JSONArray();
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        try {
            jsonArray = baseService.baseSelectAll(String.valueOf(userinfo.getUser().getOrgId().get(0)));
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonArray;
    }

    /**
     * 增加上架规则信息
     *
     * @param entity
     * @return
     */


    @ApiOperation(value = "增加上架规则信息", response = JSONObject.class, notes = "增加上架规则信息")
    @PostMapping(value = "/add")
    public @ResponseBody
    JSONObject addWmsShelfRule(@RequestBody WmsShelfRule entity,HttpSession session) {
        // JSONObject json = new JSONObject();
        JSONObject json = getJSONObject();
        entity.setId(UUIDUtils.uuid());
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);//用户信息
        String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库id
        entity.setCkid(ckid);
        entity.setCreate_man(userinfo.getUser().getName());
        try {
            Integer count = baseService.getCountByMc(entity.getGzmc(),entity.getGzdm(),ckid);
            if (count == 0) {
                 baseService.baseInsertSelective(entity);
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"增加上架规则成功","", JSONObject.fromObject(entity).toString());
            } else {
                return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"增加上架规则失败","上架规则名称或规则代码重复","");
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"增加上架规则失败",e.getMessage(),"");

        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"增加上架规则成功","", JSONObject.fromObject(entity).toString());
        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"增加上架规则失败",e.getMessage(),"");
        }
    }


}
